/**
 * 
 */
package ikeuilib;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;



/**
 * @author ikechan
 *
 */
public class TableColorEditor extends AbstractCellEditor implements TableCellEditor, ActionListener{
	private static final long serialVersionUID = 1L;
	private Color currentColor;
	private JButton button;
	private JColorChooser colorChooser;
	private JDialog dialog;
	private JTable currentTable;
	private static final String EDIT = "edit";

	public TableColorEditor() {
		button = new JButton();
		button.setActionCommand(EDIT);
		button.addActionListener(this);
		button.setBorderPainted(false);
		colorChooser = new JColorChooser();
		dialog = JColorChooser.createDialog(button,"Color",true,
					colorChooser,this,null);
		dialog.setAlwaysOnTop(true);
	}

	public void actionPerformed(ActionEvent e) {
		if (EDIT.equals(e.getActionCommand())) {
			button.setBackground(currentColor);
			colorChooser.setColor(currentColor);
			dialog.setVisible(true);
			fireEditingStopped();
		} else {
			currentColor=colorChooser.getColor();
			currentTable.setValueAt(currentColor,currentTable.getSelectedRow(),
					currentTable.getSelectedColumn());
			currentTable.removeEditor();
		}
	}

	public Object getCellEditorValue() {
		return currentColor;
	}

	public Component getTableCellEditorComponent(JTable table,Object value,boolean isSelected,
					int row,int column) {
		currentColor = (Color)value;
		currentTable=table;
		return button;
    }

	public void setCurrentColor(Color currentColor) {
		this.currentColor = currentColor;
	}

	public static void main(String[] args){
		JFrame f=new JFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JTable tb=new JTable();
		final DefaultTableModel model=new DefaultTableModel(){
			private static final long serialVersionUID = 1L;
			@Override
			public boolean isCellEditable(int arg0, int arg1) {
				return (arg1==1);
			}
			@Override
			public Class<?> getColumnClass(int arg0) {
				if(arg0==1){
					return Color.class;
				}else{
					return super.getColumnClass(arg0);
				}
			}
			
		};
		model.setColumnCount(2);
		model.setRowCount(1);
		model.setColumnIdentifiers(new String[]{"Name","Value"});
		tb.setDefaultEditor(Color.class, new TableColorEditor());
		tb.setDefaultRenderer(Color.class,  new TableColorRenderer(true));
		model.setValueAt("Color", 0, 0);
		model.setValueAt(Color.RED, 0, 1);
		tb.setModel(model);
		f.getContentPane().setLayout(new BorderLayout());
		f.getContentPane().add(new JScrollPane(tb),BorderLayout.CENTER);
		f.setTitle("Test");
		f.setSize(300,200);
		f.setLocationRelativeTo(null);
		f.setVisible(true);
	}
}
